<?php
/**
 * Created by PhpStorm.
 * User: Thans
 * Date: 2018/8/12
 * Time: 17:17
 */

namespace app\admin\service;

use app\common\model\LanguageModel;
use think\Db;

class LanguageService
{
    public function getList($page = '', $limit = '', $keyword = '')
    {
        $language = new LanguageModel;
        $where    = [];
        if ($keyword) {
            $where[] = ['name|code', 'like', '%' . $keyword . '%'];
        }
        $res   = $language->getList($where, $page, $limit);
        $total = $language->getToal($where);
        if ($res) {
            return ['code' => 1001, 'msg' => '获取成功', 'data' => $res, 'total' => $total];
        } else {
            return ['code' => 4001, 'msg' => '未获取到数据', 'data' => ''];
        }
    }

    public function getLanguage()
    {
        $language        = new LanguageModel;
        $where['status'] = '1';
        $res             = $language->getList($where, '', '', 'is_default desc,id desc', 'id,name,code,is_default');
        if ($res) {
            return ['code' => 1001, 'msg' => '获取成功', 'data' => $res];
        } else {
            return ['code' => 4001, 'msg' => '未获取到数据', 'data' => ''];
        }
    }
    /**s
     * @title 语言保存
     * $data=>'name','code','is_default','status'
     *
     */
    public function save($data)
    {
        $language = new LanguageModel;
        if ($language::where('name', $data['name'])->find()) {
            return ['code' => 4001, 'msg' => '语言名称存在重复', 'data' => ''];
        }
        if ($language::where('code', $data['code'])->find()) {
            return ['code' => 4002, 'msg' => '语言代码存在重复', 'data' => ''];
        }
        Db::startTrans();
        $data['is_default'] = empty($data['is_default']) ? 0 : 1;
        if (!empty($data['is_default'])) {
            $language::where('is_default', 1)->setField('is_default', '0');
        }
        $data['status'] = empty($data['status']) ? 0 : 1;
        $language->allowField(true)->save($data);
        if ($language->id) {
            Db::commit();
            return ['code' => 1001, 'msg' => '新增成功', 'data' => $language->id];
        }
        Db::rollback();
        return ['code' => 4003, 'msg' => '新增失败', 'data' => ''];
    }
    public function update($id, $data)
    {
        $language = new LanguageModel;
        if ($language::where('name', $data['name'])->where('id', 'neq', $id)->find()) {
            return ['code' => 4001, 'msg' => '语言名称存在重复', 'data' => ''];
        }
        if ($language::where('code', $data['code'])->where('id', 'neq', $id)->find()) {
            return ['code' => 4002, 'msg' => '语言代码存在重复', 'data' => ''];
        }
        Db::startTrans();
        $data['is_default'] = empty($data['is_default']) ? 0 : 1;
        if (!empty($data['is_default'])) {
            $language::where('is_default', 1)->setField('is_default', '0');
        }
        $data['status'] = empty($data['status']) ? 0 : 1;
        $language->allowField(true)->isUpdate(true)->save($data);
        if ($language->id) {
            Db::commit();
            return ['code' => 1001, 'msg' => '修改成功', 'data' => $language->id];
        }
        Db::rollback();
        return ['code' => 4003, 'msg' => '修改失败', 'data' => ''];
    }
    public function delete($id)
    {
        $language = new LanguageModel;
        $res      = $language->where('id', $id)->find();
        if (!$res) {
            return ['code' => 4001, 'msg' => '参数错误', 'data' => ''];
        }
        if ($res['is_default']) {
            return ['code' => 4002, 'msg' => '默认语言不能删除，请先更换默认语言', 'data' => ''];
        }
        $res = $language->where('id', $id)->delete();
        if ($res) {
            return ['code' => 1001, 'msg' => '删除成功', 'data' => ''];
        } else {
            return ['code' => 4003, 'msg' => '删除失败', 'data' => ''];
        }
    }
    public function getById($id)
    {
        $language = new LanguageModel;
        $res      = $language::where('id', $id)->find();
        if ($res) {
            return ['code' => 1001, 'msg' => '获取成功', 'data' => $res];
        }
        return ['code' => 4001, 'msg' => '不存在对应语言', 'data' => ''];
    }
    public function toggle($id)
    {
        $language = new LanguageModel;
        $res      = $language::where('id', $id)->find();
        if ($res) {
            $res = $language::where('id', $id)->setField('status', $res['status'] ? 0 : 1);
            if ($res) {
                return ['code' => 1001, 'msg' => '状态切换成功', 'data' => $res];
            }
            return ['code' => 4002, 'msg' => '状态切换失败', 'data' => ''];
        } else {
            return ['code' => 4001, 'msg' => '不存在对应语言', 'data' => ''];
        }
    }

}
